import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

/**
 * 
 */

/**
 * @author Long
 *
 */
public class Main {
	public static String defaultPath = "C:/VN_FI/";
	public static String rootName = "VN_FI";
	public static String name1 = "HSX";
	public static String name2 = "VSD";
	public static String name3 = "HNX";
	private static final String sampleText= "VN_FI_hsx_141120";
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//System.out.println(Arrays.toString(getDateCurrentWeek()));
		loadConfig();
		String[] listName = {name1,name2,name3};
		createFolder(defaultPath, rootName, listName);
		Desktop desktop = Desktop.getDesktop();
		desktop.open(new File(defaultPath));
	}
	
	public static void createFolder(String defaultPath, String rootName, String[] name) throws FileNotFoundException, IOException {
		String[] dates = getDateCurrentWeek();
		for (int i=0; i < name.length; i++) {
			for (int j =0; j < dates.length; j++) {
				String path = defaultPath+"/"+name[i]+"/"+rootName+"_"+name[i]+"_"+dates[j];
				new File(path).mkdirs();
				String filePath = path+"/"+rootName+"_"+name[i]+"_"+dates[j]+".doc";
				String content = rootName+"_"+name[i]+"_"+dates[j];
				createMSWord(filePath, content);
			}
		}
		
	}
	
	public static String[] getDateCurrentWeek() {
		Calendar now = Calendar.getInstance();

		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");

	    String[] days = new String[5];
	    int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 2; //add 2 if your week start on monday
	    now.add(Calendar.DAY_OF_MONTH, delta );
	    for (int i = 0; i < 5; i++)
	    {
	    	days[i] = format.format(now.getTime());
	        now.add(Calendar.DAY_OF_MONTH, 1);
	    }
		return days;
	}
	
	public static void loadConfig() throws IOException {
		Properties config = new Properties();
		String configFileName = "config.properties";
		InputStream input = null;
		String configPath = System.getProperty("user.dir")+"\\config\\"+configFileName;
		
		try {
			input = new FileInputStream(configPath);
			config.load(input);
		} catch (IOException e) {
			  
		}
		
		// get the property value and print it out
		if (config.getProperty("defaultPath") != null) {
			defaultPath = config.getProperty("defaultPath");
		}
		if (config.getProperty("rootName") != null) {
			rootName = config.getProperty("rootName");
		}
		if (config.getProperty("name1") != null) {
			name1 = config.getProperty("name1");
		}
		if (config.getProperty("name2") != null) {
			name2 = config.getProperty("name2");
		}
		if (config.getProperty("name3") != null) {
			name3 = config.getProperty("name3");
		}
	}
	
	public static void createMSWord (String path, String realText) throws FileNotFoundException, IOException {
		DocWriter doc = new DocWriter(path);
		doc.replaceTxt(sampleText, realText);
		doc.writeDocument();
	}

}
